package com.fhz.day018;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.fhz.day018.service.MusicService;

public class MainActivity extends AppCompatActivity {

    private MusicService.MyBinder binder;
    private ListView lv;
    private SeekBar seekBar;
    private ImageView songImage;
    private TextView songTitle;
    private ImageView pre;
    private ImageView pauseRestart;
    private ImageView next;
    private ImageView model;
    private MusicService musicService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();



        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100);


//        lv.setAdapter();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                binder.callPlay(position);
                musicService.play(position);
            }
        });

//        seekBar.setMax();
        seekBar.setProgress(musicService.getCurrentPostion());
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
//        stopService(intent)

        ServiceConnection serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                binder = (MusicService.MyBinder) service;
                musicService = binder.getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);



        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unbindService();
    }

    public void pre(View view) {
        musicService.pre();
    }

    public void pause_restart(View view) {

    }

    public void next(View view) {
        musicService.next();
    }

    public void model(View view) {
    }

    private void initView() {
        lv = (ListView) findViewById(R.id.lv);
        seekBar = (SeekBar) findViewById(R.id.seek_bar);
        songImage = (ImageView) findViewById(R.id.song_image);
        songTitle = (TextView) findViewById(R.id.song_title);
        pre = (ImageView) findViewById(R.id.pre);
        pauseRestart = (ImageView) findViewById(R.id.pause_restart);
        next = (ImageView) findViewById(R.id.next);
        model = (ImageView) findViewById(R.id.model);
    }
}